package com.sgb.kjwl.view.ui.fragment.manage;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentEnterpriseServicesBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.view.ui.activity.manage.WebsiteInfoActivity;
import com.sgb.kjwl.view.ui.fragment.enterprise.MakeOverForMessageFragment;
import com.sgb.kjwl.view.ui.fragment.enterprise.ServiceForMessageFragment;
import com.sgb.kjwl.viewmodel.manage.EnterpriseServicesModel;
import com.sgb.kjwl.viewmodel.shopping.ShopMachinekChildModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import javax.inject.Inject;

/**
 * @author 张驰
 * @date 2021/6/21 17:10
 * @Description 经营业务的企服需求板块
 * @name com.sgb.kjwl.view.ui.fragment.manage
 */
public class EnterpriseServicesFragment extends AppLazyFragment {

    @Inject
    EnterpriseServicesModel viewModel;
    private String websiteNo; //网站或个人No
    private int mTypeIndex = 0; // 初始化选中tab
    private FragmentEnterpriseServicesBinding binding;
    private String websiteType; //1 企业网站 2 团队网站  0 个人网站

    public static EnterpriseServicesFragment newInstance(String websiteNo, String websiteType, int position) {
        EnterpriseServicesFragment fragment = new EnterpriseServicesFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putString("websiteType", websiteType + "");
        args.putInt("childPosition", position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_enterprise_services, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getString("websiteType");
            mTypeIndex = getArguments().getInt("childPosition", 0);
        }
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        viewModel.mFragments.clear();
        viewModel.mFragments.add(MakeOverForMessageFragment.newInstance(websiteNo,websiteType));
        if(!websiteType.equals(WebsiteInfoActivity.WEBSITE_FOR_PERSONAL+"")){
            viewModel.mFragments.add(0, ServiceForMessageFragment.newInstance(websiteNo,websiteType));
            binding.tabLayout.setVisibility(View.VISIBLE);
            binding.viewLine.setVisibility(View.VISIBLE);
        }else{
            binding.tabLayout.setVisibility(View.GONE);
            binding.viewLine.setVisibility(View.GONE);
        }
        SlidingFragmentPagerAdapter mAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), viewModel.mFragments, viewModel.gettabNames(websiteType));
        binding.viewpager.setAdapter(mAdapter);
        binding.viewpager.setOffscreenPageLimit(viewModel.gettabNames(websiteType).length);
        binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.viewpager.setCurrentItem(0);

    }

    @Override
    protected void onLazyLoad() {

    }


}
